/*
 *@本JS功能：班级管理
 *@本js作者：杨锡君
 *@编写时间：2017年10月31日
 */
$(function() {
	var Alert1 = new Alert();
	var confirm1 = new Confirm();
	confirm1.init();
	var loading1 = new Loading();
	loading1.init();
	var $tableBox = $(".box");
	function tabelModel(opt,state) {
		var str = "<div>";
		var str1 = "";
		if(state == 0){
			str1 = "<div class=\"courseTitle\"><i class=\"iconfont\">"+ opt.courseTitle +"</i><span class=\"courseName\">"+ opt.courseName +"</span><div data-courseid="+ opt.courseId +" class=\"fr detailBtn\">班级详情 <img src=\"pc/images/ico_doubleRow.png\"></div></div>";

        }else{
            str1 ="<div class=\"courseTitle\"><span class=\"courseName\">"+ opt.className +"</span><div data-classid="+ opt.classId +" data-classname='"+  opt.className +"' data-courseid="+ opt.courseId +" class=\"fr detailBtn\">学生详情 <img src=\"pc/images/ico_doubleRow.png\"></div></div>";

        }
		var str2 = "<div class=\"clear\"></div>"
            +"<div class=\"tableSection\">"
            // +"<table class=\"tableOne\"  border=\"0\" cellspacing=\"0\" cellpadding=\"0\">"
            //
            // +"</table>"
            +"<table  class=\"tableTwo\"  border=\"0\" cellspacing=\"0\" cellpadding=\"0\">"
            +"<tr>"
            +"<th class=\"col-lg-2 stuSum\">学习人数</th>"
            +"<th colspan='2' class=\"col-lg-2\">问题交流</th>"
            +"<th colspan='2' class=\"col-lg-2\">实验时长</th>"
            +"<th colspan='3' class=\"col-lg-3\">平均成绩</th>"
            +"</tr>"
            +"<tr>"
            +"<th class=\"col-lg-2 stuSum\">学习人数</th>"
            +"<th class=\"col-lg-1\">提问数</th>"
            +"<th class=\"col-lg-1\">回答数</th>"
            +"<th class=\"col-lg-1\">总时长</th>"
            +"<th class=\"col-lg-1\">平均时长</th>"
            +"<th class=\"col-lg-1\">平时</th>"
            +"<th class=\"col-lg-1\">考试</th>"
            +"<th class=\"col-lg-1\">总成绩</th>"
            +"</tr>"
            +"<tr>"
            +"<td class=\"col-lg-2\">"+ opt.stucount +"人</td>"
            +"<td class=\"col-lg-1\">"+ opt.problemcount +"</td>"
            +"<td class=\"col-lg-1\">"+ opt.answercount +"</td>"
            +"<td class=\"col-lg-1\">"+ Number((opt.sumLong/3600).toFixed(2)) +"h</td>"
            +"<td class=\"col-lg-1\">"+  Number((opt.avgLong/3600).toFixed(2)) +"h</td>"
            +"<td class=\"col-lg-1\">"+ opt.avgPerexam +"分</td>"
            +"<td class=\"col-lg-1\">"+ opt.avgExam +"分</td>"
            +"<td class=\"col-lg-1\">"+ opt.avgSource +"分</td>"
            +"</tr>"
            +"</table>"
            +"</div>"
            +"<div class=\"container\" data-dimension=\"110\" data-text="+parseInt(opt.passRate*100)+"%"+" data-info=\"及格率\"  data-width=\"10\" data-fontsize=\"20\" data-percent="+ parseInt(opt.passRate*100) +" data-fgcolor=\"#5ac0f0\" data-bgcolor=\"#d2d2d2\"></div>"
            +"<div class=\"container\" data-dimension=\"110\" data-text="+parseInt(opt.taskRate*100)+"%"+" data-info=\"作业提交率\"  data-width=\"10\" data-fontsize=\"20\" data-percent="+ parseInt(opt.taskRate*100) +" data-fgcolor=\"#2dc7c9\" data-bgcolor=\"#d2d2d2\"></div>"
            +"<div class=\"container\" data-dimension=\"110\" data-text="+parseInt(opt.expreRate*100)+"%"+" data-info=\"实验报告提交率\"  data-width=\"10\" data-fontsize=\"20\" data-percent="+ parseInt(opt.expreRate*100) +" data-fgcolor=\"#f28e42\" data-bgcolor=\"#d2d2d2\"></div>"
            +"<div class=\"clear\"></div>"
            +"</div>";
		var str3 = "<hr>";
        if(state == 1){
            return str + str1 + str2;
		}else{
        	return str + str1 + str2 + str3;
		}
    }
    var selectModel = "<div style='height: 60px;' class=\"funBox\">"
                +"<div class=\"fl inputRight\"><label>课程类型:&ensp;</label>"
                +"<select class=\"publicSelect\" id=\"courseType\">"
                +"<option value=\"3\">请选择课程类型</option>"
                +"<option value=\"0\">未结课</option>"
                +"<option value=\"1\">已结课</option>"
                +"</select>"
                +"</div>"
                +"<div class=\"fl publicBtn searchBtn\">搜索</div>"
                +"</div>";
	var classMana = {
		init: function() {
			this.bindEvent();
            maxLengthLimit.init("publicText");
		},
		bindEvent: function() {
			var self = this;
			var json = {
				"pageNow": 1,
				"pageSize": 5,
                "classEndMark":3
			};
			self.tabelList(json);
		},
		tabelList: function(jsons) {
			var self = this;
			var json = JSON.stringify(jsons);
			loading1.show();
			$.ajax({
				type: "post",
				url: "getCouStatisticsByTeaId",
				data: {
					"jsonStr": json
				},
				async: true,
				success: function(msg) {
					loading1.hide();
					msg = JSON.parse(msg);
					$tableBox.empty();
					if(msg.result == "success"){
                        if(msg.couStatisticsMap.finalList!=null){
                            if(msg.couStatisticsMap.finalList.length != 0) {
                                //列表
                                var tabel = "";
                                $.map(msg.couStatisticsMap.finalList, function(opt) {
                                    tabel += tabelModel(opt,0);
                                });
                                $tableBox.append(tabel);
                                self.skipEvent();
                                $('.container').circliful();
                            }
                        } else {
                            var trImg = "<div><img style='margin: 80px auto 20px;' src='pc/images/ico_noneData.png'><p style='font-size:26px;color:#8a8a8a;width:100%;text-align:center;'>暂无数据</p></div>";
                            $tableBox.append(trImg)
                        }
                        var sumpage = msg.couStatisticsMap.totalPage;
                        if(sumpage == 0){
                            sumpage = 1;
                        }
                        var nowpage = 1;
                        var pagenumber = 5;
                        $(".tcdPageCode").createPage({
                            pageCount: sumpage, //总页数
                            current: nowpage, //当前页数
                            pageNumber: pagenumber, //每页显示数量
                            backFn: function(p) {
                                nowpage = p;
                                if(nowpage >= sumpage) {
                                    nowpage = sumpage;
                                }
                                var jsonStr = jsons;
                                jsonStr.pageNow = nowpage;
                                loading1.show();
                                $.ajax({
                                    type: "post",
                                    url: "getCouStatisticsByTeaId",
                                    data: {
                                        "jsonStr":JSON.stringify(jsonStr)
                                    },
                                    async: true,
                                    success: function(msg) {
                                        loading1.hide();
                                        msg = JSON.parse(msg);
                                        //列表
                                        $tableBox.empty();
                                        var tabel = "";
                                        $.map(msg.couStatisticsMap.finalList, function(opt) {
                                            tabel += tabelModel(opt,0);
                                        });
                                        $tableBox.append(tabel);
                                        self.skipEvent();
                                        $('.container').circliful();
                                    }
                                });
                            }
                        });
                    }else{
                        if(msg.failMsg != undefined){
                            if(msg.failMsg == "暂无权限"){
                                parent.location.reload(true);
                            }else{
                                Alert1.content = msg.failMsg;
                                Alert1.init();
                                Alert1.show();
                            }
                        }else{
                            if (msg.errorMsg != "" && msg.errorMsg != undefined) {
                                Alert1.content = msg.errorMsg;
                            } else {
                                Alert1.content = "获取失败";
                            }
                            Alert1.init();
                            Alert1.show();
                        }
                    }


				}
			});

		},
		skipEvent:function(){
			var self = this;
			var courseid = 0;
            $(".detailBtn").off("click").click(function(){
            	$(".wrap").css("overflow","hidden");
                var jsonStr = JSON.stringify({
                    "courseId":Number($(this).attr("data-courseid")),
                    "classEndMark":3
                });
            	self.getClassData(jsonStr);
            	courseid = $(this).attr("data-courseid");
				$(".masking").show();
				$(".dataBox").append($(this).parent().parent().clone());
                $(".dataBox").append("<hr>"+selectModel+"<div class='classBox'></div>");
                $('.dataBox .container').circliful();
                $(".fixBox").addClass("anti");
                $(".dataBox .detailBtn").eq(0).remove();
                $.each($(".dataBox .container"),function (opt) {
                	$(this).children("canvas").eq(0).remove();
                    $(this).children("span").eq(1).remove();
                    $(this).children("span").eq(0).remove();
				});
                $(".searchBtn").off("click").on("click", function() {
                    var jsonStr = JSON.stringify({
                        "courseId":Number(courseid),
                        "classEndMark":Number($("#courseType").val())
                    });
                    self.getClassData(jsonStr,Number($("#courseType").val()));
                });
            });
            $(".exitDataBtn").off("click").click(function () {
                $(".wrap").css("overflow","");
                $(".dataBox").empty();
                $(".masking").hide();
                $(".fixBox").removeClass("anti");
            });

		},
		getClassData:function (jsons,classEndMark) {
			var json =jsons;
			loading1.show();
			$.ajax({
				url:"getCouStatisticsByTeaIdDetail",
				method:"post",
				data:{
					"jsonStr":json
				},
				success:function (msg) {
				    loading1.hide();
					msg = JSON.parse(msg);
                    if(msg.couStatisticsDetailMap.finalList.length != 0) {
                        //列表
                        var tabel = "";
                        $.map(msg.couStatisticsDetailMap.finalList, function(opt) {
                            tabel += tabelModel(opt,1);
                        });
                        $(".classBox").html(tabel);
                        $('.classBox .container').circliful();
                        if(classEndMark == 1){
                            $(".classBox .stuSum").html("已学习人数");
                        }else if(classEndMark == 0){
                            $(".classBox .stuSum").html("正在学习人数");
                        }else{
                            $(".classBox .stuSum").html("学习人数");
                        }
                    }else{
                        var trImg = "<div><img style='margin: 80px auto 20px;' src='pc/images/ico_noneData.png'><p style='font-size:26px;color:#8a8a8a;width:100%;text-align:center;'>暂无数据</p></div>";
                        $(".classBox").html(trImg);
                    }
                    $(".dataBox .detailBtn").off("click").click(function(){
                        var json = JSON.stringify({
                            "pageName":"stuStatistics",
                            "paramData":{
                                "classId":$(this).attr("data-classid")
                            }
                        });
                        $(".mainNav",parent.document).children("div:nth-of-type(2)").attr("data-num","1").children("a").addClass("leftClick").end().siblings("div").attr("data-num","0").children("a").removeClass("leftClick");
                        window.location.href = "skipPage?jsonStr="+json;

                    });
                }
			});
        }
	};
	classMana.init();
});
